package org.example.likou150.hash;

// https://leetcode.cn/problems/happy-number/description/?envType=study-plan-v2&envId=top-interview-150
public class IsHappy {
    public static void main(String[] args) {
        IsHappy isHappy=new IsHappy();
        System.out.println(isHappy.isHappy(2));;
    }

    public boolean isHappy(int n) {
        int fast=n;
        int slow=n;
        do {
            fast = getNumResult(getNumResult(fast));
            slow = getNumResult(slow);
            if(fast==1) return true;
        } while (fast!=slow);
        return fast==1;
    }

    private int getNumResult(int n){
        int sum=0;
        while (n > 0){
            sum += (n % 10)*(n % 10);
            n /= 10;
        }
        return sum;
    }

}
